#!/bin/bash

> total_result
> HAVE_SERVICE
> HAVE_NO_SERVICE

all_proj=$1
for proj in ${all_proj//,/ }
do
	pkglist=`osc ls ${proj}`
	pkgcount=`osc ls ${proj} | wc -l`
	echo "${proj} ${pkgcount}" >> total_result
	for pkg in ${pkglist}
	do
		osc api -X GET /source/${proj}/${pkg}/_service
		if [ $? -eq 0 ];then
			echo "${proj} ${pkg}" >> HAVE_SERVICE
		else
			echo "${proj} ${pkg}" >> HAVE_NO_SERVICE
		fi
	done
	have=`cat HAVE_SERVICE | grep "${proj} " | wc -l`
	no_have=`cat HAVE_NO_SERVICE | grep "${proj} " | wc -l`
	echo "HAVE_SERVICE:${have} NO_SERVICE:${no_have}" >> total_result
	echo "=======================" >> total_result
done
echo "########## CHECK TOTAL RESULTS ##########"
cat total_result
echo "#########################################"

if [ `cat HAVE_NO_SERVICE | wc -l` -eq 1 ] && [ `grep -c "openEuler-latest-release" HAVE_NO_SERVICE` -eq 1 ];then
	echo "########## HAVE NO SERVICE PACKAGES##########"
	cat HAVE_NO_SERVICE
	rm -fr total_result HAVE_NO_SERVICE HAVE_SERVICE
	exit 0
elif [ ! -s HAVE_NO_SERVICE ];then
	rm -fr total_result HAVE_NO_SERVICE HAVE_SERVICE
	exit 0
else
	echo "########## HAVE NO SERVICE PACKAGES##########"
	cat HAVE_NO_SERVICE
	rm -fr total_result HAVE_NO_SERVICE HAVE_SERVICE
	exit 1
fi
